#Data analysis for lab experiment carried out Feb-May 2012
#Kris-Stella Trump kstrump@gmail.com

#install required packages
library(Hmisc)
library(foreign)
library(Zelig)


#import data. Note that this version of the data is cleaned; only usable data is included here. Raw data is included in the separate "rawdata" file. 
data <- read.csv("Exp1 - cleandata.csv", na.strings="")

#Look at data
#for variable definitions see codebook
names(data)
dim(data)

#####Prepare variables 

#Q13.3 is experimental condition
#1 = 1-9
#2 = 4-6
summary(data$Q13.3)

#Q11.4_1 is question on how much participant would give the winner
summary(data$Q11.4_1)

#belief in just world - create average
#Q4.1_1 - Q4.1_8
data$bjw <- (data$Q4.1_1 + data$Q4.1_2 + data$Q4.1_3 + data$Q4.1_4 + data$Q4.1_5 + data$Q4.1_6 + data$Q4.1_7 + data$Q4.1_8) / 8
summary(data$bjw)

#for later calculations, easier if equal condition is designated 'control' i.e. 0, and unequal remains 1
data$treat <- data$Q13.3==1
summary(data$treat)


#create variable for conservative
data$cons <- (data$Q2.5>3)
sum(data$cons) #11

#partisanship
#republican
sum(data$Q2.4>3, na.rm=T) #11

#for Zelig to run smoothly:
data$treat <- as.numeric(data$treat)

#Race dummy (white)
data$white <- data$Q2.3==1

#separate out unequal condition
condition19 <- data[data$treat==1,]

#separate out equal condition
condition46 <- data[data$treat==0,]


####### Descriptive stats

dim(data) #53 participants
sum(data$treat) #26 in treatment condtion

#Age
mean(data$Q2.1) #30
range(data$Q2.1) #15-56
median(data$Q2.1) #24

#Gender - female
sum(data$Q2.2==1) #28

#Race 
sum(data$Q2.3==1) #29 white
sum(data$Q2.3==2) #9 African American
sum(data$Q2.3>2) #15 other
sum(data$Q2.3==3) #6 Hispanic
sum(data$Q2.3==4) #7 Asian
sum(data$Q2.3>4) #2 other

#belief in just world
mean(data$bjw) #3.24
range(data$bjw) #1.25-5.625

#Balance table
balance <- zelig(treat ~ Q2.1 + Q2.2 + white + bjw + cons, model="ls", data=data)
summary(balance)

#Age
mean(condition19$Q2.1)
mean(condition46$Q2.1)

#Gender
sum(condition19$Q2.2==2)/26
sum(condition46$Q2.2==2)/27

#Race
sum(condition19$Q2.3==1)/26
sum(condition46$Q2.3==1)/27

#BJW
mean(condition19$bjw)
mean(condition46$bjw)

#Partisanship
sum(condition19$cons==1)/26
sum(condition46$cons==1)/27


#####Main results
#####


#Test whether outcome variable differs between conditions
z.out <- zelig(Q11.4_1 ~ treat, model="ls", data=data)
summary(z.out)

#simulate expected values

x.out <- setx(z.out, treat=c(0,1))
s.out <- sim(z.out, x=x.out)
summary(s.out)


#create graphic for the results. the tests above bring out the means and CIs we want to plot

mean <- c(6.15, 7.77) 
lci <- c(5.73, 7.33)
uci <- c(6.60, 8.19)
rawresults <- as.data.frame(cbind(mean, uci,lci))
#Check the resulting data frame
rawresults

library(Hmisc)
#First save layout of the plot, to extract midpoints of the bars, used in addition of ci
mp <- barplot(rawresults$mean, space=0.5, ylim=c(5,10), main="Money allocation to the winner", names.arg=c("$6 condition", "$9 condition"))

#write a pdf file with the graph
pdf("labresults.pdf")
barplot(rawresults$mean, space=0.5, ylim=c(5,10), main="Laboratory experiment: Money allocation to the winner", names.arg=c("$6 condition", "$9 condition"), col=c("grey40","lightgrey"), xpd=F)
axis(1, at=c(0,4))
title(ylab="$ allocated to winner")
errbar(mp, rawresults$mean,rawresults$uci,rawresults$lci, add=T, pch=NA)
dev.off()



#### fairness: Q11.3 completely unfair 1 completely fair 7

t.test(condition19$Q11.3, condition46$Q11.3)
#fairness estimates differ between the two conditions
